from flask import Blueprint, request
from conn_mysql import ConnectMysql


app_process_order = Blueprint('app_process_order', 'api')

@app_process_order.route('/app_process_order', methods=['POST'])
def process_order():

    order_id = request.form.get("order_id")
    dirver_id = request.form.get("dirver_id")
    order_status = request.form.get("order_status")
    admin_remarks = request.form.get("admin_remarks")
    # user_remarks = request.form.get("user_remarks")

    sql = "SELECT * FROM order_status WHERE order_id=%s"
    insert_sql = """
        INSERT INTO 
        order_status(order_id, dirver_id, order_status, admin_remarks)
        VALUES
        (%s, %s, %s, %s);
    """
    update_sql = """
        UPDATE 
        order_status 
        SET
        dirver_id=%s, order_status=%s, admin_remarks=%s
        WHERE
        order_id=%s;

    """

    with ConnectMysql() as c:
        result = c.select_one(sql, (order_id,))
    
        if result:

            c.update_info(update_sql, (dirver_id, order_status, admin_remarks, order_id))

        else:

            c.insert_info(insert_sql, (order_id, dirver_id, order_status, admin_remarks))

    return {
        'status' : 1,
        'errorCode': None,
        'errorMsg': None,
        'resultBody': '提交成功',
    }





